﻿Imports System.Data.OleDb
Public Class resultdetail
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If IsPostBack = False Then
            If Request.Params("fixtureId") = "" Then Response.Redirect("resultlist.aspx")
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("soccerConnectionString").ConnectionString)
            Dim SqlString As String = "Select * From Comments Where fixtureId = @f1"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", Request.Params("fixtureId"))
            oleDbConn.Open()
            Dim dataReader = cmd.ExecuteReader()
            GridView2.DataSource = dataReader
            GridView2.DataBind()

            Dim fixturesViewedSql As String = "Insert Into FixturesViewed(viewedBy, fixtureId, dateViewed) Values (@f1,@f2,@f3)"
            Dim cmd2 As OleDbCommand = New OleDbCommand(fixturesViewedSql, oleDbConn)
            cmd2.CommandType = CommandType.Text
            cmd2.Parameters.AddWithValue("@f1", User.Identity.Name)
            cmd2.Parameters.AddWithValue("@f2", Request.Params("fixtureId"))
            cmd2.Parameters.AddWithValue("f3", DateTime.Now.ToString("dd/MM/yyyy HH:mm"))
            cmd2.ExecuteNonQuery()

         End If

    End Sub

    Protected Sub btn_AddComment_Click(sender As Object, e As EventArgs) Handles btn_AddComment.Click
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("SoccerConnectionString").ConnectionString)
        Dim SqlString As String = "Insert Into Comments(author, commentSubject, commentText, fixtureId,commentDate) Values (@f1,@f2,@f3,@f4,@f5)"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        'Dim myGUID, myGUID2 As New System.Guid
        'myGUID = Guid.NewGuid()
        'myGUID2 = Guid.NewGuid()
        'Dim newFileName = myGUID.ToString() + ".jpg"
        'Dim newFileName2 = myGUID2.ToString() + ".jpg"

        'If F_Picture.HasFile Then
        'F_Picture.SaveAs(Request.MapPath("pictures") & "/" & newFileName)
        'Using OriginalImage = Image.FromFile(Request.MapPath("pictures") & "/" & newFileName)
        'Using ResizedImage As New Bitmap(OriginalImage, 500, 500)
        'ResizedImage.Save(Request.MapPath("pictures") & "/" & newFileName2, Drawing.Imaging.ImageFormat.Jpeg)

        'End Using
        'End Using
        'End If
        cmd.CommandType = CommandType.Text
        'cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
        cmd.Parameters.AddWithValue("@f2", tb_CommentSubject.Text)
        cmd.Parameters.AddWithValue("@f3", tb_commentText.Text)
        cmd.Parameters.AddWithValue("@f4", Request.Params("fixtureId"))
        cmd.Parameters.AddWithValue("@f5", DateTime.Now.ToString("dd/MM/yyyy HH:mm"))
        oleDbConn.Open()
        cmd.ExecuteNonQuery()
        'Response.Redirect("resultconfirm.aspx")
        'fill GridView2
        Dim fillComments As String = "Select * From Comments Where fixtureId = @f1"
        Dim fillCommentsCmd As OleDbCommand = New OleDbCommand(fillComments, oleDbConn)
        fillCommentsCmd.CommandType = CommandType.Text
        fillCommentsCmd.Parameters.AddWithValue("@f1", Request.Params("fixtureId"))
        Dim dataReader = fillCommentsCmd.ExecuteReader()
        GridView2.DataSource = dataReader
        GridView2.DataBind()

        tb_CommentSubject.Text = ""
        tb_commentText.Text = ""
        'tb_Author.Text = ""

    End Sub

    Protected Sub bn_TagFixture_Click(sender As Object, e As EventArgs) Handles bn_TagFixture.Click
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("soccerConnectionString").ConnectionString)
        Dim SqlString As String = "Insert Into FixtureTagLink(fixtureId, tagId) Values (@f1,@f2)"
        oleDbConn.Open()
        For Each cb As ListItem In cb_Tags.Items
            If cb.Selected Then
                'populate the FixtureTagLink table
                Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
                cmd.CommandType = CommandType.Text
                cmd.Parameters.AddWithValue("@f1", Request.Params("fixtureId"))
                cmd.Parameters.AddWithValue("@f2", cb.Value)
                cmd.ExecuteNonQuery()
            End If
        Next
        'add new tags
        If tb_NewTag.Text = "" Then Return
        Dim newTags = tb_NewTag.Text.Split(",")
        For Each newTag In newTags
            'add new tag to table
            Dim newTagSql As String = "Insert Into FixtureTags(Tag) Values (@f1)"
            Dim cmd As OleDbCommand = New OleDbCommand(newTagSql, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", newTag)
            cmd.ExecuteNonQuery()
            'find id of the new tag
            Dim getLastIdCmd As OleDbCommand = New OleDbCommand("Select Top 1 tagId From FixtureTags Order By tagId Desc", oleDbConn)
            Dim dr = getLastIdCmd.ExecuteReader()
            dr.Read()
            Dim lastId = dr.GetValue(0)
            'associate id with fixture id
            Dim newcmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            newcmd.CommandType = CommandType.Text
            newcmd.Parameters.AddWithValue("@f1", Request.Params("fixtureId"))
            newcmd.Parameters.AddWithValue("@f2", lastId)
            newcmd.ExecuteNonQuery()

        Next

    End Sub
End Class